﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace TrayTool
{
  /////////////////////////////////////////////////////////////////////////////
  //
  //  ContextMenu class
  //
  class ContextMenu : ContextMenuStrip, IComponent
  {
    #region Events

    public event EventHandler HideClick;


    #endregion
    #region Fields

    private ToolStripSeparator m_separator = new ToolStripSeparator();
    private int m_count = 0;


    #endregion
    #region Constructors

    ///////////////////////////////////////////////////////////////////////////
    //
    //  Constructor
    //
    public ContextMenu()
      : base()
    {
      SuspendLayout();

      m_separator.Visible = false;
      Items.Add(m_separator);

      ToolStripMenuItem hide = new ToolStripMenuItem("Hide");
      hide.Click += delegate(object s, EventArgs e)
      {
        if (HideClick != null)
        {
          HideClick(this, EventArgs.Empty);
        }
      };
      Items.Add(hide);

      ToolStripMenuItem exit = new ToolStripMenuItem("Exit");
      exit.Click += delegate(object s, EventArgs e)
      {
        Application.Exit();
      };
      Items.Add(exit);

      ResumeLayout();
    }


    #endregion
    #region Add / Remove

    ///////////////////////////////////////////////////////////////////////////
    //
    //  Add()
    //
    public void Add(ToolStripMenuItem item)
    {
      Container.Add(item);
      Items.Insert(Items.IndexOf(m_separator), item);
      if (m_count++ == 0)
      {
        m_separator.Visible = true;
      }
    }


    ///////////////////////////////////////////////////////////////////////////
    //
    //  Remove()
    //
    public void Remove(ToolStripMenuItem item)
    {
      Items.Remove(item);
      if (--m_count == 0)
      {
        m_separator.Visible = false;
      }
    }


    #endregion
  }
}
